Conversation
…filter_read
Two bugs in FilterUtilCall prevented source-filter modules that use the
method-filter form (e.g. CPAN's Switch.pm) from working:
1. Method filters were unimplemented. applyFilters() had a TODO branch
that returned the source unchanged whenever filter_add was called
with a blessed ref instead of a coderef, so `use Switch` never
rewrote any switch/case blocks and produced a bare syntax error.
Now we look up ${pkg}::filter via InheritanceResolver and drive it
the same way we drive closure filters: call it with $self as the
first arg, accumulate $_, loop until status <= 0.
2. filter_read($blocksize) wrongly terminated block reads at the first
"\n", turning block mode into line mode. Switch::filter asks for
1_000_000 bytes expecting the whole source, but only got one line
per call, which made Text::Balanced::_match_codeblock fail with
"Bad switch statement (problem in the code block?)". Removed the
early newline break so block mode reads up to $blocksize bytes.
Verified with `./jcpan -t Switch`: Files=3, Tests=590, Result: PASS
(was 0/3 test files before, all failing with syntax errors). `make`
still passes with no unit-test regressions.
Generated with [Devin](https://cli.devin.ai/docs)
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
8c003b7 to
8769858
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Makes CPAN's
Switchmodule (and any other module that uses method-style source filters viaFilter::Util::Call) work on PerlOnJava../jcpan -t Switchbefore: all 3 test files fail withsyntax error ... near ""(0/3)../jcpan -t Switchafter:Files=3, Tests=590, Result: PASS.Root cause
Switch.pmis a source filter. In itsimport()it callsfilter_add({})(a hashref), which Filter::Util::Call blesses into the caller's package — a method filter. Itsfilter()method doesfilter_read(1_000_000)to slurp the whole source, then rewritesswitch/caseblocks viaText::Balanced.Two bugs in
FilterUtilCall.javabroke this:applyFilters()had an explicit// TODO: Implement method filter callingbranch that just returned the source unchanged, soswitch (...) { case ... }was passed verbatim to the parser.filter_read($blocksize)was broken in block mode. The read loop broke out on the first"\n", turning block reads into line reads.Switch::filterasked for 1,000,000 bytes but got only"\n"then"switch (1) {\n", soText::Balanced::_match_codeblockfailed withBad switch statement (problem in the code block?).Fix
In
src/main/java/org/perlonjava/runtime/perlmodule/FilterUtilCall.java:${pkg}::filterviaInheritanceResolver.findMethodInHierarchy, call it with the blessed filter object as$self, accumulate$_, and loop until status ≤ 0 — matching the closure-filter loop that was already there.if (line.endsWith("\n")) break;sofilter_read($n)reads up to$nbytes instead of up to one line.Test plan
make— BUILD SUCCESSFUL, all unit tests pass, no regressions./jcpan -t Switch—Files=3, Tests=590, Result: PASSt/given.t— 293/293t/nested.t— 4/4t/switch.t— 293/293Generated with Devin